package client.comm.baoding.ui

import client.comm.commlib.toast.toast
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.page.OrderFragmentPageAdapter
import client.comm.baoding.alipay.AlipayUtils
import client.comm.baoding.api.bean.SuccessEvent
import client.comm.baoding.databinding.ActivityMeorderBinding
import client.comm.baoding.ui.vm.MeOrderViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GoodsOrderActivity : BaseMiddleActivity<MeOrderViewModel, ActivityMeorderBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_meorder
    }

    val adapter: OrderFragmentPageAdapter by lazy {
        OrderFragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): MeOrderViewModel = createViewModel()

    override fun initView() {
        EventBus.getDefault().register(this)
        binding.event = this
        binding.viewPager.adapter = adapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)

        when (intent.getIntExtra("index", 0)) {
            0 -> {
//                binding.tabWrap.getChildAt(0).isSelected = true
            }
            1 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(1, false)
                }, 10)
            }
            2 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(2, false)
                }, 10)
            }
            3 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(3, false)
                }, 10)
            }
            4 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(4, false)
                }, 10)
            }
        }
//        binding.viewPager.clearOnPageChangeListeners()
//        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
//            override fun onPageScrolled(
//                position: Int,
//                positionOffset: Float,
//                positionOffsetPixels: Int
//            ) {
//            }
//
//            override fun onPageSelected(position: Int) {
//                for ((i, c) in binding.tabWrap.children.withIndex()) {
//                    if (i != position) {
//                        c.isSelected = false
//                    } else {
//                        c.isSelected = true
//                    }
//                }
//            }
//
//            override fun onPageScrollStateChanged(state: Int) {
//
//            }
//
//        })
    }

    var tradeNo: String = ""
    override fun observerUI() {
        viewModel.orderPayCall.observe(
            this, {
                it?.let {
                    if (it.isSuccess) {
                        tradeNo = it.data!!.trade_no
                        AlipayUtils(this).pay(it.data!!.pay_url)
                    }
                }
            }
        )
        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("取消成功")
                    viewModel.pageOne.value = 0
                    viewModel.holdOne()
                    viewModel.pageTwo.value = 0
                    viewModel.holdTwo()
                    viewModel.pageZero.value = 0
                    viewModel.holdZero()
                }
            }
        })

        viewModel.qrshCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("收货成功")
                    viewModel.pageOne.value = 0
                    viewModel.holdOne()
                    viewModel.pageTwo.value = 0
                    viewModel.holdTwo()
                    viewModel.pageThree.value = 0
                    viewModel.holdThree()
                }
            }
        })

        viewModel.checkOrderPayCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("支付成功")
                    viewModel.pageOne.value = 0
                    viewModel.holdOne()
                    viewModel.pageTwo.value = 0
                    viewModel.holdTwo()
                    viewModel.pageZero.value = 0
                    viewModel.holdZero()
                }
            }
        })

    }

    //支付成功的回调
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun paySuccess(event: SuccessEvent?) {
        viewModel.check_order_pay(tradeNo)

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


}